data type and data structure in Python

Python
Author

Tony Duan

Published

July 6, 2023

1 bulid-in data Structures

1.1 singular

Code
a=1
type(a)
int
Code
a=1.3
type(a)
float
Code
a='hell'
type(a)
str

1.2 list

Code
a=[1,2,3]

a
[1, 2, 3]
Code
type(a) 
list
Code
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana','apple']

1.2.1 find length of the list with len()

Code
len(fruits)
8

1.2.2 find how many time in the list with count()

Code
fruits.count('apple')
3

1.2.3 find locaiton of on the list with index()

show the first ‘apple’ index. python list start at 0

Code
fruits.index('apple')
1

all ‘apple’ in the list

Code
[index for index, value in enumerate(fruits) if value == 'apple']
[1, 5, 7]

1.3 reverse the list

Code
fruits.reverse()
fruits
['apple', 'banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']

1.3.1 sort the list

Code
fruits.sort()
fruits
['apple', 'apple', 'apple', 'banana', 'banana', 'kiwi', 'orange', 'pear']

1.3.2 add element on the list

Code
fruits.append('grape')
fruits
['apple',
 'apple',
 'apple',
 'banana',
 'banana',
 'kiwi',
 'orange',
 'pear',
 'grape']

1.4 drop last element

Code
fruits.pop()

fruits
['apple', 'apple', 'apple', 'banana', 'banana', 'kiwi', 'orange', 'pear']

1.4.1 List Comprehensions

using loop:

Code
squares = []
for x in range(10):
  squares.append(x**2)
  
squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

using List Comprehensions

Code
squares = [x**2 for x in range(10)]
squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

1.5 Tuples

Code
fruits = ('orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana','apple')

fruits
('orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana', 'apple')
Code
type(fruits)
tuple

tuple can not be modified.

1.6 Sets

A set is an unordered collection with no duplicate elements.

Code
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}

basket
{'apple', 'banana', 'orange', 'pear'}
Code
type(basket)
set

1.7 Dictionaries

Code
tel = {'jack': 4098, 'sape': 4139}

tel
{'jack': 4098, 'sape': 4139}
Code
type(tel)
dict
Code
tel['jack']
4098

2 numpy data Structures(matrix in python)

NumPy is the fundamental package for scientific computing in Python. It is a Python library that provides a multidimensional array object

Python doesn’t have a built-in type for matrices. However, we can treat a list of a list as a matrix

Code
A = [[1, 4, 5, 12], 
    [-5, 8, 9, 0],
    [-6, 7, 11, 19]]
    
A
[[1, 4, 5, 12], [-5, 8, 9, 0], [-6, 7, 11, 19]]

numpy Array

Code
import numpy as np

A = np.array([[1, 2, 3], [3, 4, 5]])
print(A)
[[1 2 3]
 [3 4 5]]
Code
type(A)
numpy.ndarray

2.0.1 selection

first row

Code
A[0]
array([1, 2, 3])

first column

Code
A[:,0]
array([1, 3])

first row and first column element

Code
A[0,0]
1
Code
A.dtype
dtype('int64')

2 row and 3 column

Code
A.shape
(2, 3)

2.0.2 reshape

Code
a=np.arange(9).reshape(3, 3)

a
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

3 pandas data Structures(dataframe in python)

4 Reference

https://docs.python.org/3/tutorial/datastructures.html#

https://numpy.org/doc/stable/user/basics.rec.html